﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SilentVoice.Models
{
    public class Event : Auditable
    {
        public Event()
        {
            this.AllowedGroups = new HashSet<Group>();
            this.Description = "";
            this.Outline = "";
        }
        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public string Description { get; set; }
        public string Outline { get; set; }
        [Required]
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }


//        [Required]
        //public virtual Account Account { get; set; }
        public int AccountId { get; set; }

       // [Required]
       // public User Owner { get; set; }
        public int OwnerId { get; set; }
        public virtual ICollection<Group> AllowedGroups { get; set; }

        //TODO: this should go to business logic
        /*public IEnumerable<ValidationResult> IsValid(ValidationContext validationContext)
        {
            //Users can only create Events in their own Account
            if (!Owner.Account.Id.Equals(Id))
            {
                yield return new ValidationResult("Owner is not part of this Event's Account", new[] { "Users" });
            }


            yield break;
        }*/

        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType().DeclaringType != this.GetType().DeclaringType)
            {
                System.Console.WriteLine("they're not the same type, {0} {1}", obj.GetType().DeclaringType.ToString(), this.GetType().DeclaringType.ToString());
                return false;
            }

            Event other = (Event)obj;

            return this.Name.Equals(other.Name)
                && this.Description.Equals(other.Description)
                && this.Outline.Equals(other.Outline)
                && this.AccountId == other.AccountId
                && this.OwnerId == other.OwnerId;
        }

        public override int GetHashCode()
        {
            return ("" + this.Name.GetHashCode() + "|" + this.OwnerId.GetHashCode()).GetHashCode();
        }

        public override string ToString()
        {
            return String.Format("{0},{1},{2},{3},{4},{5}", Name, Description, Outline, AccountId, OwnerId, Id);
        }
    }
}